package com.example.administrator.testproject.MVP.Recilte;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import com.example.administrator.testproject.MVP.Adapter.RecileAdapter;
import com.example.administrator.testproject.MVP.Base.BaseActivity;
import com.example.administrator.testproject.MVP.Base.BaseQuickAdapter;
import com.example.administrator.testproject.MVP.Entity.DetectListItemDto;
import com.example.administrator.testproject.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class TestMainActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener{

    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.fab)
    FloatingActionButton fab;

    private List<DetectListItemDto> mData = new ArrayList<>();
    private RecileAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        setSupportActionBar(toolbar);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    protected int setContentViews() {
        return R.layout.recyclertest_layout;
    }

    public void initView(){
        list.setLayoutManager(new LinearLayoutManager(this));
        list.setHasFixedSize(true);
        mAdapter = new RecileAdapter(R.layout.test_list_item, mData);
        //设置加载动画
        mAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        //设置是否自动加载以及加载个数
//        mAdapter.openLoadMore(bookType.getPageLength(),true);
        //将适配器添加到RecyclerView
        list.setAdapter(mAdapter);
        //设置自动加载监听
        mAdapter.setOnLoadMoreListener(this);
        mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Log.i("TAGSS", "title : " + mAdapter.getItem(position).getTitle());
                startActivity(new Intent(getApplicationContext(), DetailPageActivity.class).putExtra("Dto", mAdapter.getItem(position)));
            }
        });

        final RetileMain retile = new RetileMain();
        try {
            retile.doDetectJandanHomePageData(new RetileMain.simpleListener() {
                @Override
                public void onListener() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mAdapter.addData(retile.getDetectUrlList());
                        }
                    });
                }

                @Override
                public void onDetailListener(String str) {

                }
            });
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void onLoadMoreRequested() {

    }
}
